package com.egg.ylt.presenter.impl;

import com.egg.ylt.Utils.API;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.pojo.BabyEntity;
import com.egg.ylt.pojo.BannerDataEntity;
import com.egg.ylt.pojo.MemberBabyEntity;
import com.egg.ylt.pojo.RelationEntity;
import com.egg.ylt.pojo.RelativeFamilyEntity;
import com.egg.ylt.presenter.IFamilyPresenter;
import com.egg.ylt.view.IFamilyView;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.common.CommonUtils;
import com.yonyou.framework.library.mvp.impl.BasePresenter;
import com.yonyou.framework.library.net.ReqCallBack;
import com.yonyou.framework.library.net.RequestManager;

import java.util.HashMap;
import java.util.List;

/**
 * Created by qiuzhenhuan on 2019/1/18.
 */

public class FamilyPresenterImpl extends BasePresenter<IFamilyView> implements IFamilyPresenter {


    @Override
    public void loadFamilyInfo(String babyId) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("companyId", Constants.COMPANYID);
        hashMap.put("token", Constants.TOKEN);
        hashMap.put("babyId", babyId);
        RequestManager.getInstance().requestPostByAsyn(API.GET_RELATIVE_LIST, hashMap, new ReqCallBack<RelativeFamilyEntity>() {

            @Override
            public void onReqSuccess(RelativeFamilyEntity result) {
                mView.showFamilyList(result);
            }

            @Override
            public void onFailure(String result) {
                mView.showException(result);
            }

            @Override
            public void onReqFailed(ErrorBean error) {
                mView.showException(error.getMsg());
            }
        });
    }

    @Override
    public void getBabyList() {

        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("companyId", Constants.COMPANYID);
        hashMap.put("token", Constants.TOKEN);
        RequestManager.getInstance().requestPostByAsyn(API.GET_BABY_LIST, hashMap, new ReqCallBack<MemberBabyEntity>() {

            @Override
            public void onReqSuccess(MemberBabyEntity result) {
                mView.showBabyList(result);
            }

            @Override
            public void onFailure(String result) {
                mView.showLoadBabyError(result);
            }

            @Override
            public void onReqFailed(ErrorBean error) {
                mView.showLoadBabyError(error.getMsg());
            }
        });
    }

    @Override
    public void removeRelative(String babyId,String relationshipId,String memberCompanyId) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("relationshipId", relationshipId);
        hashMap.put("memberCompanyId", memberCompanyId);
        hashMap.put("babyId", babyId);
        hashMap.put("companyId", Constants.COMPANYID);
        hashMap.put("token", Constants.TOKEN);
        RequestManager.getInstance().requestPostByAsyn(API.DELETE_RELATIVE_BY_ID, hashMap, new ReqCallBack<Object>() {

            @Override
            public void onReqSuccess(Object result) {
                mView.showRemoveSuccess();
            }

            @Override
            public void onFailure(String result) {
                mView.showException(result);
            }

            @Override
            public void onReqFailed(ErrorBean error) {
                mView.showException(error.getMsg());
            }
        });
    }

    @Override
    public void addRelative(String babyId, String relationshipId) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("token", Constants.TOKEN);
        hashMap.put("companyId", Constants.COMPANYID);
        hashMap.put("babyId", babyId);
        hashMap.put("relationId", relationshipId);
        RequestManager.getInstance().requestPostByAsyn(API.ADD_BABY_USER_RELATIONSHIP, hashMap, new ReqCallBack<String>() {

            @Override
            public void onReqSuccess(String result) {
                mView.bindRelativeSuccess();
            }

            @Override
            public void onFailure(String result) {
//                mView.showError(result);
                mView.showException(result);
            }

            @Override
            public void onReqFailed(ErrorBean error) {
//                mView.showException(error.toString());
                mView.showException(error.getMsg());
            }
        });
    }


    public void getRelationList() {
        mView.showDialogLoading("正在加载中...");
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("token", Constants.TOKEN);
        hashMap.put("companyId", Constants.COMPANYID);
        RequestManager.getInstance().requestGetByAsyn(API.GET_RELATION_LIST, hashMap, new ReqCallBack<RelationEntity>() {
            @Override
            public void onReqSuccess(RelationEntity result) {
                mView.dismissDialogLoading();
                mView.updateRelationList(result.getList());
            }

            @Override
            public void onFailure(String result) {
                mView.dismissDialogLoading();
                mView.showError(result);
            }

            @Override
            public void onReqFailed(ErrorBean error) {
                mView.dismissDialogLoading();
                mView.showError(error.getMsg());
            }
        });
    }
}
